<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="所属会议" prop="metting_id">
        <el-select
          v-model="form.metting_id"
          clearable
          filterable
          style="width: 100%"
        >
          <el-option
            v-for="(type, i) in mettingList"
            :key="i"
            clearable="true"
            :label="type.name"
            :value="type.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="嘉宾名称" prop="name">
        <el-input v-model.trim="form.name" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model.trim="form.mobile" />
      </el-form-item>
      <el-form-item label="微信号" prop="weixin">
        <el-input v-model.trim="form.weixin" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model.trim="form.email" />
      </el-form-item>
      <el-form-item label="公司名称" prop="company">
        <el-input v-model.trim="form.company" />
      </el-form-item>
      <el-form-item label="职位" prop="position">
        <el-input v-model.trim="form.position" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { defineComponent, getCurrentInstance, reactive, toRefs } from 'vue'
  import { doEdit } from '@/api/orderCustomer'
  import { baseURL } from '@/config'

  export default defineComponent({
    name: 'OrderCustomerEdit',
    emits: ['fetch-data'],
    // 处于生命周期函数 beforeCreate 和 Created 两个钩子函数之前的函数
    // 执行 setup 时，组件实例尚未被创建（在 setup() 内部，this 不会是该活跃实例的引用，即不指向vue实例，Vue 为了避免我们错误的使用，直接将 setup函数中的this修改成了 undefined）
    setup(props, { emit }) {
      const { proxy } = getCurrentInstance()
      console.log(window.location.host)
      const state = reactive({
        formRef: null,
        mettingList: [],
        uploadActionUrl: baseURL + 'upload/ajaxUploads',
        form: {
          id: '',
          name: '',
          mobile: '',
          weixin: '',
          email: '',
          company: '',
          position: '',
        },
        rules: {
          metting_id: [
            { required: true, trigger: 'blur', message: '请选择所属会议' },
          ],
          name: [
            { required: true, trigger: 'blur', message: '请输入嘉宾名称' },
          ],
          mobile: [
            { required: true, trigger: 'blur', message: '请输入手机号' },
            { pattern: /^1\d{10}$/, message: '请输入正确手机号' },
          ],
          company: [
            { required: true, trigger: 'blur', message: '请输入公司名称' },
          ],
          position: [
            { required: true, trigger: 'blur', message: '请输入职位' },
          ],
          email: [
            {
              type: 'email',
              trigger: 'change',
              message: '请输入正确的邮箱地址',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
        roleList: [],
      })
      const showEdit = (row, mettingList) => {
        if (row.id) {
          state.title = '编辑嘉宾'
          state.form = Object.assign({}, row)
        } else {
          state.title = '添加嘉宾'
          state.form = Object.assign({}, row)
        }
        state.mettingList = mettingList
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            state.form.is_invite = 1
            const { msg } = await doEdit(state.form)
            proxy.$baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }
      const handleSuccess = (response) => {
        if (response.code == 0) {
          state.form.logo = response.data[0].path
        }
      }
      const dateChange = (val) => {
        console.log(val)
      }

      // 与模板一起使用：需要返回一个对象 (在setup函数中定义的变量和方法最后都是需要 return 出去的 不然无法再模板中使用)
      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
        handleSuccess,
        dateChange,
      }
    },
  })
</script>
<style lang="scss" scoped>
  .tip {
    margin: 0px;
    color: red;
  }
  :deep() {
    .el-upload-list {
      display: none;
    }
  }
</style>
